package at.fhooe.mc.msports;

import java.util.Vector;

import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;

public class GPSManager implements LocationListener {

	Criteria criteria;
	LocationProvider provider;
	Vector listeners = new Vector();

	public GPSManager() {
		try {
			criteria = new Criteria();
			criteria.setHorizontalAccuracy(1000);
			criteria.setVerticalAccuracy(1000);
			criteria.setCostAllowed(false);

			criteria.setAltitudeRequired(true);
			criteria.setPreferredPowerConsumption(Criteria.NO_REQUIREMENT);
			provider = LocationProvider.getInstance(criteria);
			provider.setLocationListener(this, 5, -1, -1);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void addListener(IGps listener) {
		listeners.addElement(listener);
	}

	public void locationUpdated(LocationProvider provider, Location location) {
		System.out.println("lat: "
				+ location.getQualifiedCoordinates().getLatitude() + " lon: "
				+ location.getQualifiedCoordinates().getLongitude() + " alt: "
				+ location.getQualifiedCoordinates().getAltitude());
		for (int i = 0; i < listeners.size(); i++) {
			((IGps) listeners.elementAt(i)).onLocationUpdate(location);
		}
	}

	public void providerStateChanged(LocationProvider provider, int newState) {

	}
}
